﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.Profile;
using Microsoft.Practices.Unity;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Vit.Web.Models;
using Vit.Common.Extensions;

namespace Vit.Web.Services
{
    public class MembershipAccountProvider : IAccountProvider
    {
        public MembershipAccountProvider()
        {
        }

        #region IAccountService Members

        public User GetCurrentUser()
        {
            User user = null;
            //if (HttpContext.Current.User != null)
            //{
            //    MembershipUser mu = System.Web.Security.Membership.GetUser();
            //    if (mu != null)
            //    {
            //        user = new User()
            //        {
            //            UserID = mu.UserName,
            //            Name = mu.UserName,
            //            IsAdmin = false,
            //            IsAnoymous = mu.IsApproved,
            //            IsAuthenticated = true,
            //        };

            //        user.UserFilesPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath + "UserFiles", user.Name);
            //    }
            //}

            if (user == null)
            {
                user = CreateAnoymousUser(VitApplication.Current.CurrentSite.Name);
            }
            return user;
        }

        public User CreateAnoymousUser(string siteName)
        {
            User user = new User()
            {
                UserID = 0,
                Name = "Anoymous",
                IsAdmin = false,
                IsAnoymous = true,
                IsAuthenticated = false
            };

            user.UserFilesPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath + "UserFiles", user.Name);

            return user;
        }

        public User GetUserByName(string siteName, string name)
        {
            return CreateAnoymousUser(siteName);
        }

        public User GetUserById(string siteName, object id)
        {
            return CreateAnoymousUser(siteName);
        }

        public bool CheckUserPermission(User user, string siteName, string area, string controller, string action)
        {
            return false;
        }

        public bool CheckUserPermission(User user, string siteName, string uri)
        {
            return false;
        }

        public bool CheckUserModelPermission(User user, string siteName, Type entityType, string propertyName)
        {
            // add model permission
            return true;
        }

        #endregion
    }
}
